package com.fms.trollalarm;

import android.app.Activity;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

import com.fms.trollalarm.controller.AlarmController;
import com.fms.trollalarm.object.AlarmModel;
import com.fms.trollalarm.object.LevelType;
import com.fms.trollalarm.object.TrollType;
import com.fms.trollalarm.utils.Common.IntentExtra;

public class AlarmDetailActivity extends Activity {
	// Layout
	private TimePicker tpTime;
	private EditText etName;
	private ToggleButton btMon;
	private ToggleButton btTue;
	private ToggleButton btWed;
	private ToggleButton btThu;
	private ToggleButton btFri;
	private ToggleButton btSat;
	private ToggleButton btSun;
	private LinearLayout llSelectTone;
	private Button btAddDone;
	private Button btDelete;
	private TextView tvTone;
	private Spinner spTrollType;
	private Spinner spTrollLevel;
	private ToggleButton btVibrate;

	// Common
	// private AlarmDBHelper dbHelper = new AlarmDBHelper(this);
	private AlarmModel alarmDetails;
	private AlarmController alarmController = new AlarmController(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm_detail);
		findViewIds();
		init();
		setEvent();
	}

	private void setEvent() {
		llSelectTone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(
						RingtoneManager.ACTION_RINGTONE_PICKER);
				startActivityForResult(intent, 1);
			}
		});

		btAddDone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				updateModelFromLayout();

				if (alarmDetails.getId() < 0) {
					alarmController.createAlarm(alarmDetails);
				} else {
					alarmController.updateAlarm(alarmDetails);
				}

				setResult(RESULT_OK);
				finish();
			}
		});

		btDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				alarmController.deleteAlarm(alarmDetails);
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	protected void updateModelFromLayout() {
		alarmDetails.setTimeMinute(tpTime.getCurrentMinute().intValue());
		alarmDetails.setTimeHour(tpTime.getCurrentHour().intValue());
		alarmDetails.setName(etName.getText().toString());
		alarmDetails.setRepeatWeekly(btSun.isChecked() || btMon.isChecked()
				|| btTue.isChecked() || btWed.isChecked() || btThu.isChecked()
				|| btFri.isChecked() || btSat.isChecked());
		alarmDetails.setRepeatingDay(AlarmModel.SUNDAY, btSun.isChecked());
		alarmDetails.setRepeatingDay(AlarmModel.MONDAY, btMon.isChecked());
		alarmDetails.setRepeatingDay(AlarmModel.TUESDAY, btTue.isChecked());
		alarmDetails.setRepeatingDay(AlarmModel.WEDNESDAY, btWed.isChecked());
		alarmDetails.setRepeatingDay(AlarmModel.THURSDAY, btThu.isChecked());
		alarmDetails.setRepeatingDay(AlarmModel.FRDIAY, btFri.isChecked());
		alarmDetails.setRepeatingDay(AlarmModel.SATURDAY, btSat.isChecked());
		alarmDetails.setTrollType(spTrollType.getSelectedItemPosition());
		alarmDetails.setLevelType(spTrollLevel.getSelectedItemPosition());
		alarmDetails.setEnabled(true);
		alarmDetails.setVibrate(btVibrate.isChecked());
	}

	private void init() {
		long id = getIntent().getExtras().getLong(IntentExtra.ID);
		if (id == -1) {
			alarmDetails = new AlarmModel();
			alarmDetails.setAlarmTone(RingtoneManager
					.getDefaultUri(RingtoneManager.TYPE_ALARM));
			spTrollType.setSelection(TrollType.DEFAULT.ordinal());
			spTrollLevel.setSelection(LevelType.EASY.ordinal());
		} else {
			btDelete.setVisibility(View.VISIBLE);
			alarmDetails = alarmController.getAlarm(id);

			tpTime.setCurrentMinute(alarmDetails.getTimeMinute());
			tpTime.setCurrentHour(alarmDetails.getTimeHour());

			etName.setText(alarmDetails.getName());

			// chkWeekly.setChecked(alarmDetails.repeatWeekly);
			btSun.setChecked(alarmDetails.getRepeatingDay(AlarmModel.SUNDAY));
			btMon.setChecked(alarmDetails.getRepeatingDay(AlarmModel.MONDAY));
			btTue.setChecked(alarmDetails.getRepeatingDay(AlarmModel.TUESDAY));
			btWed.setChecked(alarmDetails.getRepeatingDay(AlarmModel.WEDNESDAY));
			btThu.setChecked(alarmDetails.getRepeatingDay(AlarmModel.THURSDAY));
			btFri.setChecked(alarmDetails.getRepeatingDay(AlarmModel.FRDIAY));
			btSat.setChecked(alarmDetails.getRepeatingDay(AlarmModel.SATURDAY));

			tvTone.setText(RingtoneManager.getRingtone(this,
					alarmDetails.getAlarmTone()).getTitle(this));
			spTrollType.setSelection(alarmDetails.getTrollType());
			spTrollLevel.setSelection(alarmDetails.getLevelType());
			btVibrate.setChecked(alarmDetails.isVibrate());
		}
	}

	private void findViewIds() {
		tpTime = (TimePicker) findViewById(R.id.tp_time);
		etName = (EditText) findViewById(R.id.et_name);
		btMon = (ToggleButton) findViewById(R.id.bt_mon);
		btTue = (ToggleButton) findViewById(R.id.bt_tue);
		btWed = (ToggleButton) findViewById(R.id.bt_wed);
		btThu = (ToggleButton) findViewById(R.id.bt_thu);
		btFri = (ToggleButton) findViewById(R.id.bt_fri);
		btSat = (ToggleButton) findViewById(R.id.bt_sat);
		btSun = (ToggleButton) findViewById(R.id.bt_sun);
		llSelectTone = (LinearLayout) findViewById(R.id.ll_select_tone);
		btAddDone = (Button) findViewById(R.id.bt_add_done);
		btDelete = (Button) findViewById(R.id.bt_delete);
		tvTone = (TextView) findViewById(R.id.tv_tone);
		spTrollType = (Spinner) findViewById(R.id.sp_troll_type);
		spTrollLevel = (Spinner) findViewById(R.id.sp_troll_level);
		btVibrate = (ToggleButton) findViewById(R.id.bt_vibrate);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alarm_detail, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case 1: {
				alarmDetails
						.setAlarmTone((Uri) data
								.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI));
				tvTone.setText(RingtoneManager.getRingtone(this,
						alarmDetails.getAlarmTone()).getTitle(this));
				break;
			}
			default: {
				break;
			}
			}
		}
	}

}
